package com.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.store.common.Result;
import com.store.dto.EmployeeCreateRequest;
import com.store.dto.EmployeeQueryRequest;
import com.store.dto.EmployeeUpdateRequest;
import com.store.dto.PasswordResetRequest;
import com.store.service.EmployeeService;
import com.store.vo.EmployeeVO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/employee")
@RequiredArgsConstructor
public class EmployeeController {
    
    private final EmployeeService employeeService;
    
    /**
     * 创建员工
     */
    @PostMapping("/create")
    public Result<Long> createEmployee(@Validated @RequestBody EmployeeCreateRequest request,
                                        HttpServletRequest httpRequest) {
        try {
            Long employeeId = employeeService.createEmployee(request);
            return Result.success("员工创建成功", employeeId);
        } catch (Exception e) {
            log.error("创建员工失败", e);
            return Result.error("创建员工失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新员工信息
     */
    @PutMapping("/{id}")
    public Result<String> updateEmployee(@PathVariable Long id,
                                          @Validated @RequestBody EmployeeUpdateRequest request,
                                          HttpServletRequest httpRequest) {
        try {
            employeeService.updateEmployee(id, request);
            return Result.success("员工信息更新成功");
        } catch (Exception e) {
            log.error("更新员工信息失败", e);
            return Result.error("更新员工信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除员工
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteEmployee(@PathVariable Long id, HttpServletRequest httpRequest) {
        try {
            employeeService.deleteEmployee(id);
            return Result.success("员工已删除");
        } catch (Exception e) {
            log.error("删除员工失败", e);
            return Result.error("删除员工失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取员工详情
     */
    @GetMapping("/{id}")
    public Result<EmployeeVO> getEmployeeDetail(@PathVariable Long id, HttpServletRequest httpRequest) {
        try {
            EmployeeVO employee = employeeService.getEmployeeDetail(id);
            return Result.success(employee);
        } catch (Exception e) {
            log.error("获取员工详情失败", e);
            return Result.error("获取员工详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 查询员工列表（分页）
     */
    @GetMapping("/list")
    public Result<IPage<EmployeeVO>> getEmployeeList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String phone,
            HttpServletRequest httpRequest) {
        try {
            EmployeeQueryRequest queryRequest = new EmployeeQueryRequest();
            queryRequest.setKeyword(keyword);
            queryRequest.setStatus(status);
            queryRequest.setPhone(phone);
            
            IPage<EmployeeVO> employeePage = employeeService.getEmployeeList(page, pageSize, queryRequest);
            return Result.success(employeePage);
        } catch (Exception e) {
            log.error("查询员工列表失败", e);
            return Result.error("查询员工列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有员工列表（不分页）
     */
    @GetMapping("/all")
    public Result<List<EmployeeVO>> getAllEmployees(HttpServletRequest httpRequest) {
        try {
            List<EmployeeVO> employees = employeeService.getAllEmployees();
            return Result.success(employees);
        } catch (Exception e) {
            log.error("获取员工列表失败", e);
            return Result.error("获取员工列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 启用/禁用员工
     */
    @PutMapping("/{id}/toggle-status")
    public Result<String> toggleEmployeeStatus(@PathVariable Long id, HttpServletRequest httpRequest) {
        try {
            employeeService.toggleEmployeeStatus(id);
            return Result.success("员工状态已更新");
        } catch (Exception e) {
            log.error("切换员工状态失败", e);
            return Result.error("切换员工状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 重置员工密码
     */
    @PutMapping("/{id}/reset-password")
    public Result<String> resetPassword(@PathVariable Long id,
                                          @Validated @RequestBody PasswordResetRequest request,
                                          HttpServletRequest httpRequest) {
        try {
            employeeService.resetPassword(id, request.getNewPassword());
            return Result.success("密码重置成功");
        } catch (Exception e) {
            log.error("重置密码失败", e);
            return Result.error("重置密码失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取员工统计信息
     */
    @GetMapping("/{id}/statistics")
    public Result<EmployeeVO> getEmployeeStatistics(@PathVariable Long id, HttpServletRequest httpRequest) {
        try {
            EmployeeVO statistics = employeeService.getEmployeeStatistics(id);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取员工统计信息失败", e);
            return Result.error("获取员工统计信息失败：" + e.getMessage());
        }
    }
}

